Divide a given number by 2^k and display the output

The dividing function also works similar to the multiply function. Only the operator changes. So the steps for dividing a number by 2^k are listed below:

  • Assign a value, say 5 to a variable ‘k’
  • Then, we have to store the value of 2^5 in another variable j.
  • Divide the contents of both k and j.
  • Display the output .(Here 160 will be the resultant output)

The Python 3 code for basic division is as follows:

Python3




k = 5
j = 2**k
  
p = k//j
  
print(p)


Output:

0

We can use ‘//’ for integer division and ‘/’ for floor division as per our requirement.And the optimized code using right shift operation will look like :

Python3




k = 5
  
print(k>>k)


Output:

0

Both the codes will result in the output ‘0’(integer division). 

Note: For more information, refer to Python Bitwise Operators.

 

All these operations already existed since history. But we forgot to practice using them. For our changing future, let’s adopt these small changes and become a part of it.



Change your way to put logic in your code – Python

Aren’t you bored coding in the same traditional logic over these years. It’s time to bring some differences. Well, be it any programming language. When we are asked to do some sort of basic programming exercises like sorting, searching, prime number checking, etc. we are all driven towards the same boat, which is the same coding logic. Now, we are in the past 2020, so let’s also take an initiative to change our thinking styles when it comes to coding. It is because, only if we think differently, we can code differently. 

 Lets dive deep into those wonderful coding tips.

Similar Reads

Check whether a given number is Odd or Even

Of course, what a simple question. We can easily check for even or odd using the modulus operator. You want to know how? Just see the below Python code snippet –...

Checking whether a given number is prime or not?

...

Multiply a given number k by 2^k and display the output

...

Divide a given number by 2^k and display the output

Those numbers which are divisible by 1 and that number only are called prime numbers. Examples for prime numbers are 2, 3, 5, 7, etc. which do not have any other factors(excluding themselves and 1). We all have been taught about the below logic for checking the prime numbers....